package eu.jpereira.trainings.designpatterns.structural.decorator.channel.decorator;

import java.util.List;

import eu.jpereira.trainings.designpatterns.structural.decorator.channel.SocialChannel;

public class WordCensor extends SocialChannelDecorator{

	private List<String> forbiddenWords;
	private String coveringSymbol = "*";
	
	public WordCensor(List<String> forbiddenWords) {
		this.forbiddenWords = forbiddenWords;
	}
	
	public WordCensor(List<String> forbiddenWords, SocialChannel decoratedChannel) {
		this.forbiddenWords = forbiddenWords;
		this.delegate = decoratedChannel;
	}

	@Override
	public void deliverMessage(String message) {
		String replacement = "";
		
		for( String forbidden : forbiddenWords){
			replacement = "";
			for( int i = 0; i < forbidden.length(); i++){
				replacement = replacement + coveringSymbol;
			}
			message = message.replaceAll(forbidden, replacement);
			
		}
		delegate.deliverMessage(message);
	}
}
